# LuckPay Web Manage System
#
# Copyright (c) 2016 Lucky Byte, Inc.
#

# 复核时，需要冻结所有的界面元素，不允许用户编辑
checking_freeze = (form) ->
    in_checking = form.form 'get value', 'in_checking'
    return if in_checking isnt 'true'

    # 修改背景色，以区分复核及录入
    $('body').css('background-color', 'black')
    freeze_form form


# 改变 form 的验证字段
update_validator = (form, inside) ->
    fields =
        merid:  optional: false, rules: [ { type: 'empty' } ]
        termid: optional: false, rules: [
            { type: 'empty' }, { type: 'doesntContain[ ]' }
        ]

    # 平台终端需要提供下列的额外信息
    if inside
        fields.tmk = optional: false, rules: [
            { type: 'minLength[32]' }, { type: 'maxLength[32]' },
            { type: 'regExp[/^[0-9a-fA-F]+$/]' }
        ]
        fields.macalgo = optional: false, rules: [ { type: 'empty' } ]

    form.form('destroy')
    form.form inline: true, fields: fields

    form.find('.field.error').removeClass 'error'
    form.find('.pointing.prompt.label').remove()


$ ->
    form = $('form.main')
    update_validator form, false

    # 如果是复核，冻结界面元素
    checking_freeze form

    # 页面数据集
    record = form.form 'get value', 'record'
    if record and record.trim().length > 0
        record = JSON.parse record
        form.form 'set value', 'record', ''
    else
        record = null

    # 隶属商户选择改变
    form.find('.dropdown.merid').dropdown
        onChange: (value, text, $choice) ->
            inside = $choice.data('inside')

            # 更新 form 验证规则
            update_validator form, inside

            form.find('.dropdown.merid').data 'inside', inside
            $mapto_term = form.find('.dropdown.mapto_term')
            if not inside
                return $mapto_term.addClass('disabled').dropdown 'clear'

            # 平台商户可以配置外联终端，查询外联终端列表
            $mapto_term.addClass 'loading'
            $.post '/terms/query/mapto_terms', {
                chnl: $choice.data('chnl')
            }, (resp) ->
                $mapto_term.removeClass 'loading'
                return if not resp.succ
                items = '<div class="item" data-value=" ">无</div>'
                for item in resp.records
                    inside = if item.inside then '平台商户' else '外联商户'

                    items += "<div class='item' data-value='#{item.uuid}'>"
                    items += "#{item.termid}"
                    color = if item.shaded then 'orange' else '#888'
                    items += "<div class='right floated'
                        style='font-size:small; color:#{color};'>#{item.mer_name} ｜ "
                    items += "#{item.mer_no} ｜ "
                    items += "#{inside} ｜ "
                    items += "#{item.mer_chnl}"
                    items += "</div>"
                    items += "</div>"
                $mapto_term.find('.menu').html items
                $mapto_term.removeClass('disabled').dropdown 'refresh'

                # 恢复关联终端的值
                if record and record.mapto_term
                    form.form 'set value', 'mapto_term', record.mapto_term

                # 如果是复核，冻结界面元素
                checking_freeze form

    # 显示/隐藏 tmk
    form.find('.icon.eyes').click ->
        $input_real = $(this).siblings('input.real')
        $input_show = $(this).siblings('input.show')
        if $(this).data('hide')
            $input_show.val $input_real.val()
            $input_show.removeAttr 'readonly'
            $(this).removeClass('unhide').addClass('hide red')
            $(this).data('hide', false)
        else
            $input_real.val $input_show.val()
            $input_show.val if $input_real.val() then '••••••••' else ''
            $input_show.attr 'readonly', 'readonly'
            $(this).removeClass('hide red').addClass('unhide')
            $(this).data('hide', true)

    # 生成终端主密钥
    form.find('a#tmk_gen').click (evt) ->
        evt.preventDefault()
        key = ''
        for i in [0..31]
            t = Math.ceil(Math.random() * 15)
            t = if t > 9 then t - 10 + 65 else t + 48
            key += String.fromCharCode(t)
        tmk_hide = form.form 'get value', 'tmk_hide'
        tmk = form.form 'get value', 'tmk'
        if tmk_hide == tmk
            form.form 'set value', 'tmk_hide', key
        form.form 'set value', 'tmk', key

    # 录入删除按钮事件
    form.find('button#input_delete').click (evt) ->
        if $(this).hasClass('has_clicked')
            $(this).removeClass('has_clicked').text('删 除')
            $.post '/terms/input/delete',
                serial: form.form('get value', 'serial'),
                uuid: form.form('get value', 'uuid')
        else
            $(this).addClass('has_clicked').text('确认删除')

    # 复核通过按钮事件
    form.find('button#pass').click ->
        if $(this).hasClass('has_clicked')
            effect_time = form.form 'get value', 'effect_time'
            if not effect_time or effect_time.trim().length == 0
                alert '请选择信息生效的时间'
                return
            $(this).removeClass('has_clicked').text('通 过')
            form.attr 'action', '/terms/check/pass'
            form.submit()
        else
            form.find('button#reject').text('拒 绝').removeClass('has_clicked')
            form.find('.fields#reject_reason').hide()
            form.find('.fields#effect_time').show()
            $(this).addClass('has_clicked').text('确认通过')

    # 复核拒绝按钮事件
    form.find('button#reject').click ->
        if $(this).hasClass('has_clicked')
            reason = form.form 'get value', 'reject_reason'
            if not reason or reason.trim().length == 0
                alert '请输入拒绝的原因，以便录入人员可以有针对性的进行修改'
                return
            $(this).removeClass('has_clicked').text('拒 绝')
            form.attr 'action', '/terms/check/reject'
            form.submit()
        else
            form.find('button#pass').text('通 过').removeClass('has_clicked')
            form.find('.fields#effect_time').hide()
            form.find('.fields#reject_reason').show()
            $(this).addClass('has_clicked').text('确认拒绝')


    # 恢复界面的值
    if record then form.form 'set values', record

    form.submit (evt) ->
        # 如果修改了 tmk_hide，需要使用修改后的值上传
        form.find('.icon.eyes').each ->
            if not $(this).data('hide')
                $input_real = $(this).siblings('input.real')
                $input_show = $(this).siblings('input.show')
                $input_real.val $input_show.val()

        if not form.form 'is valid'
            evt.preventDefault()
            form.find('button#ok').removeClass 'loading'
            return false
